package com.miguo.notification;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.miguo.bean.GoodsBean;
import com.miguo.db.GoodsDB;
import com.miguo.framework.utils.Log;
import com.miguo.receivers.NotiReceiver;

import java.util.Calendar;

public class Notification {
    private static Notification notification;

    public static Notification getInstance() {
        notification = new Notification();
        return notification;
    }

    public void setNotification(Context context, boolean add, GoodsBean goods) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, NotiReceiver.class);
        Bundle bundle = new Bundle();
        // bundle.putLong("content", content);

        long starttime = goods.getBegin_time();
        bundle.putLong("begin_time", goods.getBegin_time());
        intent.putExtras(bundle);
        PendingIntent pi = PendingIntent.getBroadcast(context, (int) starttime,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (add) {
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis(1000L * (starttime - 300L));
            am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
            return;
        }

        if (GoodsDB.getInstance(context).getNotiCountByStartTime(
                goods.getBegin_time()) == 0) {
            am.cancel(pi);
        }
    }

}
